package tv.scolopender.timertask;

import java.util.TimerTask;
import java.util.concurrent.ExecutionException;

import tv.scolopender.activity.SwipeMainActivity;
import tv.scolopender.application.ScoloApp;
import tv.scolopender.asynctask.NewsAsyncTask;
import tv.scolopender.asynctask.RegisterUserAsyncTask;
import tv.scolopender.asynctask.ScoloNewestAsyncTask;
import tv.scolopender.model.User;
import android.app.Activity;
import android.content.Intent;
import android.provider.Settings.Secure;

public class SplashTask extends TimerTask {
	private final Activity activity;

	public SplashTask(Activity activity) {
		super();
		this.activity = activity;
	}

	@Override
	public void run() {
		ScoloApp scoloApp = (ScoloApp) activity.getApplicationContext();
		User user = new User(Secure.getString(activity.getContentResolver(),
				Secure.ANDROID_ID));
		new RegisterUserAsyncTask(user).execute();

		ScoloNewestAsyncTask scolosNewestAsyncTask = new ScoloNewestAsyncTask(
				activity);
		scolosNewestAsyncTask.execute(0, 5);
		NewsAsyncTask newsAsyncTask = new NewsAsyncTask(activity);
		newsAsyncTask.execute();
		try {
			scoloApp.setScolos(scolosNewestAsyncTask.get());
			scoloApp.setNews(newsAsyncTask.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		Intent i = new Intent(activity, SwipeMainActivity.class);
		activity.startActivity(i);
		activity.finish();
	}
}